package com.hl233.framework.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.hl233.framework.entity.KafkaData1Entity;
import com.hl233.framework.entity.KafkaDataEntity;
import com.hl233.framework.mapper.KafkaData1Mapper;
import com.hl233.framework.singleton.ConnectionPool;
import org.springframework.beans.factory.annotation.Autowired;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

/**
 * @author 张振强15065206763
 * @create 2024-09-06
 */
public class KafkaDataConsumer implements Runnable {

    private final String name;

    public KafkaDataConsumer(String name) {
        this.name = name;
    }

    private ConnectionPool connectionPool = ConnectionPool.getInstance();


    @Override
    public void run() {
        synchronized (this) {
            while (true) {
                Connection conn = null;
                Statement stmt = null;
                try {
                    conn = connectionPool.getConnection();
//                    KafkaDataEntity bread = KafkaDataQueue.take();
//                    System.out.println("拿到这个东西" + bread);
                    String sql = "INSERT INTO kafka_data_1 (context, createtime, val) " +
                            "VALUES ('textrrrrr','" + DateUtil.now() + "',2333)";
                    System.out.println(name + "///查看这个sql:" + sql);
                    System.out.println("查看这个连接拿到了么:" + conn);
                    stmt = conn.createStatement();
                    System.out.println("这个东西能用么" + stmt == null);
                    int i = stmt.executeUpdate(sql);
//                Thread.sleep(100);
                    System.out.println("查看执行结果" + i);
//                    System.out.println("剩余数据量" + KafkaDataQueue.BREAD_STORE.size());
                    stmt.close();

                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("发生异常??" + e);
                } finally {
                    if (conn != null) connectionPool.releaseConnection(conn);

                }
            }
        }
    }

    public static void main(String[] args) {
        ConnectionPool connectionPool = ConnectionPool.getInstance();
        Connection conn = null;
        Statement stmt = null;
        try {
            conn = connectionPool.getConnection();
            String sql = "INSERT INTO kafka_data_1(context, createtime, val) VALUES ('ces 0001','" + DateUtil.now() + "',15)";
            System.out.println("查看这个sql:" + sql);
            System.out.println("查看这个连接拿到了么:" + conn);
            stmt = conn.createStatement();
            int i = stmt.executeUpdate(sql);
            System.out.println("查看执行结果" + i);
            stmt.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("发生异常??" + e);
        } finally {
            if (conn != null) connectionPool.releaseConnection(conn);

        }
    }
}
